<?php
/**
 * Created by PhpStorm.
 * User: server
 * Date: 2019/5/29
 * Time: 9:26
 */

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;
use think\Config;
use think\Paginator;
use think\Request;
use think\Validate;

class EarningStatistics extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {

        $data = input();
        $map = [];

        $listRows = $this->request->param('numPerPage') ? 20 : Config::get("paginate.list_rows");

        $list = Db::name('dispatch_traveler_group')
            ->alias('dtg')
            ->join('tp_route r', 'dtg.route_id=r.id', 'left')
            ->join('tp_admin_user au', 'dtg.driver_user_id=au.id', 'left')
            ->join('tp_driver_hotel_expense dhe', 'dtg.id=dhe.group_id', 'left')
            ->join('tp_dispatch_insurance di', 'dtg.id=di.group_id', 'left')
            ->where($map)
            ->where('dtg.id', 465)
            ->field('dtg.*,r.title,au.realname,dhe.total_price,di.insurance_money')
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        $new_list = $list->toArray()['data'];

        foreach ($new_list as $k => $v) {
            // 酒店信息
            $new_list[$k]['hotel'] = Db::name('hotel_accommodation')
                ->alias('ha')
                ->join('tp_hotel h', 'ha.hotel_id=h.id', 'left')
                ->where('ha.group_id', $v['id'])
                ->field('ha.hotel_id,ha.group_id,ha.balance_payment,h.hotel_name')
                ->select();
            foreach ($new_list[$k]['hotel'] as $k2 => $v2) {
                // 计算酒店房款总金额
                $new_list[$k]['hotel_total'] = Db::name('hotel_accommodation')
                    ->where('group_id', $v2['group_id'])
                    ->sum('balance_payment');
            }

            // 出行旅客信息
            $new_list[$k]['order_traveler'] = Db::name('order_traveler')
                ->alias('ot')
                ->join('tp_order o', 'ot.oid=o.id', 'left')
                ->where('ot.group_id', $v['id'])
                ->group('ot.oid')
                ->field('ot.*,o.payment_fee,o.platform,o.conscience_money_channel,o.conscience_money_kind,o.conscience_money,o.expenditure_channel,o.expenditure_kind,o.expenditure_money')
                ->select();

            foreach ($new_list[$k]['order_traveler'] as $k2 => $v2) {
                // 下单渠道
                $new_list[$k]['order_traveler'][$k2]['platform'] = Db::name('platform')
                    ->where('platform_name',$v2['platform'])
                    ->value('name');
                // 计算订单发票金额
                $new_list[$k]['order_traveler'][$k2]['insurance_money'] = Db::name('order_traveler')
                    ->where('oid', $v2['oid'])
                    ->sum('insurance_money');
                // 计算派车单发票总金额
                $new_list[$k]['total_insurance_money'] = $new_list[$k]['total_insurance_money'] + $new_list[$k]['order_traveler'][$k2]['insurance_money'];
                // 计算派车单支付总金额
                $new_list[$k]['order_total_money'] = $new_list[$k]['order_total_money'] + $v2['payment_fee'];
                // 计算订单旅客人数
                $new_list[$k]['order_traveler'][$k2]['num'] = Db::name('order_traveler')
                    ->where('oid', $v2['oid'])
                    ->count();
            }
        }

//        halt($new_list);
        $this->view->assign("list", $new_list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {

            $data = input();

            // 修改订单数据
            foreach ($data['order_info'] as $k => $v) {
                Db::name('order')
                    ->where('id', $k)
                    ->update([
                        'conscience_money_channel' => $v['conscience_money_channel'],
                        'conscience_money_kind' => $v['conscience_money_kind'],
                        'conscience_money' => $v['conscience_money'],
                        'expenditure_channel' => $v['expenditure_channel'],
                        'expenditure_kind' => $v['expenditure_kind'],
                        'expenditure_money' => $v['expenditure_money'],
                    ]);
            }

            // 计算补缴和支出合计
            $other_conscience_money = 0;
            $other_expenditure_money = 0;
            foreach ($data['order_info'] as $k => $v) {
                $other_conscience_money = $other_conscience_money + $v['conscience_money'];
                $other_expenditure_money = $other_expenditure_money + $v['expenditure_money'];
            }

            // 修改司陪房价格
            Db::name('driver_hotel_expense')
                ->where('group_id', $data['id'])
                ->setField('total_price', $data['total_price']);

            // 修改酒店价格
            foreach ($data['hotel_price'] as $k => $v) {
                Db::name('hotel_accommodation')
                    ->where('id', $k)
                    ->setField('total_price', $v);
            }

            // 修改派车单数据
            Db::name('dispatch_traveler_group')
                ->where('id', $data['id'])
                ->update([
                    'this_settlement_money' => $data['this_settlement_money'],
                    'other_deduct_money' => $data['other_deduct_money'],
                    'other_conscience_money' => $other_conscience_money,
                    'other_expenditure_money' => $other_expenditure_money
                ]);

            // 修改司机保险价格
            Db::name('dispatch_insurance')
                ->where('group_id', $data['id'])
                ->setField('insurance_money', $data['insurance_money']);

            return ajax_return_adv('编辑成功');
        } else {
            $data = input();

            $list = Db::name('dispatch_traveler_group')
                ->alias('dtg')
                ->join('tp_route r', 'dtg.route_id=r.id', 'left')
                ->join('tp_admin_user au', 'dtg.driver_user_id=au.id', 'left')
                ->join('tp_driver_hotel_expense dhe', 'dtg.id=dhe.group_id', 'left')
                ->join('tp_dispatch_insurance di', 'dtg.id=di.group_id', 'left')
                ->where('dtg.id', $data['id'])
                ->field('dtg.*,r.title,au.realname,dhe.total_price,di.insurance_money')
                ->find();

//            halt($list);

            // 酒店信息
            $list['hotel'] = Db::name('hotel_accommodation')
                ->alias('ha')
                ->join('tp_hotel h', 'ha.hotel_id=h.id', 'left')
                ->where('ha.group_id', $list['id'])
                ->field('ha.id,ha.hotel_id,ha.group_id,ha.total_price,h.hotel_name')
                ->select();
            foreach ($list['hotel'] as $k2 => $v2) {
                // 计算酒店房款总金额
                $list['hotel_total'] = Db::name('hotel_accommodation')
                    ->where('group_id', $v2['group_id'])
                    ->sum('total_price');
            }

            // 出行旅客信息
            $list['order_traveler'] = Db::name('order_traveler')
                ->alias('ot')
                ->join('tp_order o', 'ot.oid=o.id', 'left')
                ->where('ot.group_id', $list['id'])
                ->group('ot.oid')
                ->field('ot.*,o.payment_fee,o.platform,o.conscience_money_channel,o.conscience_money_kind,o.conscience_money,o.expenditure_channel,o.expenditure_kind,o.expenditure_money')
                ->select();
            // 派车单的订单数量
            $list['order_traveler_count'] = count($list['order_traveler']);

            foreach ($list['order_traveler'] as $k2 => $v2) {
                // 计算订单保险总金额
                $list['order_traveler'][$k2]['insurance_money'] = Db::name('order_traveler')
                    ->where('oid', $v2['oid'])
                    ->sum('insurance_money');
                // 计算派车单保险总金额
                $list['total_insurance_money'] = $list['total_insurance_money'] + $list['order_traveler'][$k2]['insurance_money'];
                // 计算派车单支付总金额
                $list['order_total_money'] = $list['order_total_money'] + $v2['payment_fee'];
                // 计算订单旅客人数
                $list['order_traveler'][$k2]['num'] = Db::name('order_traveler')
                    ->where('oid', $v2['oid'])
                    ->count();
            }

//            halt($list);
            $this->view->assign("vo", $list);
            return $this->view->fetch();
        }
    }
}